<?php 
include('header.php');

// check if query string was passed if not send back to overview
if (($_SERVER['QUERY_STRING'] == "") && (!isset($_POST['id']))) { header('Location: index.php'); }

// Check if id in query string
if (isset($_GET['id'])) { $id = $_GET['id']; }

// get posted vars and insert into db (update run updates)
if (isset($_POST['run_update'])) {
        $run_update = $_POST['run_update'];
	$id = $_POST['id'];
        mysql_query('UPDATE server SET run_update=' . $run_update . ' WHERE id=' . $id);
	if ($run_update == 1) {
		$msg = '<div class="updated">Server set to update at next runtime.</div>';
	} else {
		$msg = '<div class="updated">Server set to not update at next runtime.</div>';
	}
}

// get posted vars and insert into db (update reboot)
if (isset($_POST['reboot'])) {
        $reboot = $_POST['reboot'];
	$id = $_POST['id'];
        mysql_query('UPDATE server SET reboot=' . $reboot . ' WHERE id=' . $id);
	if ($reboot == 1) {
		$msg .= '<div class="updated">Server set to reboot after update.</div>';
	} else {
		$msg .= '<div class="updated">Server set to not reboot after update.</div>';
	}
}

// Query db for server information
$server = mysql_query('SELECT * FROM server WHERE id=' . $id);

$server_info = mysql_fetch_array($server);
 
// set vars
$hostname = $server_info['hostname'];
$packages = $server_info['packages'];
$update = $server_info['run_update'];
$exit_stat = $server_info['exit_stat'];
$error_msg = $server_info['error_msg'];
$reboot = $server_info['reboot'];
?>

<h2><?php echo $hostname; ?> Extended Server Information</h2>
<h3><a href="#available">Available Updates</a> | <a href="#update">Update</a></h3>
<?php if (isset($msg)) { echo $msg; } ?>
<hr />

<?php if ($exit_stat !=0) { ?>
<div class="error">
	<p>There was an error the last time the process ran.</p>
	<p>The error was: <?php echo $error_msg; ?></p>
</div>
<?php } ?>

<h3><a name="available"></a>Available Updates (<?php if ($packages) { echo count(explode(' ',$packages)); } else { echo '0'; } ?>) <small><small><a href="#top">_top_</a></small></small></h3>
<?php // create table in scrollbox with available updates ?>
<div class="scrollbox">
	<table class="smalltable">
		<tbody>
			<?php if ($packages) { ?>
				<?php $n = 0; ?>
				<?php foreach (explode(' ', $packages) as $package) { ?>
				<?php
					if($n % 2 == 1) {
						$row_class = 'row1';
					} else {
						$row_class = 'row2';
					}
				?>
				<tr>
					<td class="<?php echo $row_class; ?>"><?php echo $package; ?></td>
				</tr>
				<?php $n++; ?>
				<?php } ?>
			<?php } else { ?>
				<tr>
					<td>No Updates Available</td>
				</tr>
			<?php } ?>
		</tbody>
	</table>
</div>

<br />
<hr />
<?php if ($authrow['level'] == 1) { ?>
<div>
	<h3><a name="update"></a>Update <small><small><a href="#top">_top_</a></small></small></h3>
	<form action="srvinfo.php" method="post">
        	Update @ Next Runtime: <select name="run_update">
                	<option <?php if ($update == 1) { echo 'selected="selected"'; } ?> value="1">Yes</option>
	                <option  <?php if ($update == 0) { echo 'selected="selected"'; } ?> value="0">No</option>
        	</select><br />
		Reboot After Update: <select name="reboot">
                        <option <?php if ($reboot == 1) { echo 'selected="selected"'; } ?> value="1">Yes</option>
                        <option  <?php if ($reboot == 0) { echo 'selected="selected"'; } ?> value="0">No</option>
                </select><br />
		<input type="hidden" name="id" value="<?php echo $id; ?>" />
	        <input type="submit" value="Submit" />
	</form>
</div>

<hr />
<?php } ?>
<?php include('footer.php'); ?>
